1 00:00:00,420 --> 00:00:07,110 If you want to start using the data store to save data in between games on the home tab, select that. 2 00:00:07,110 --> 00:00:10,640 Go to this game settings, click the cog. 3 00:00:10,650 --> 00:00:13,950 If you haven't saved the game, it'll tell you that you need to do that. 4 00:00:13,950 --> 00:00:16,920 But we've already saved our game at least one time. 5 00:00:16,920 --> 00:00:22,560 Let's go to security and then enable studio access to API services. 6 00:00:22,560 --> 00:00:28,560 The data store is one of those services we need to enable this and then hit save. 7 00:00:29,690 --> 00:00:33,470 Let's start by saving off our leaderboard data to the data store. 8 00:00:33,470 --> 00:00:37,160 So we have our points, our kills and our deaths in between games. 9 00:00:37,190 --> 00:00:39,910 I'm going to go to server script service here and Explorer. 10 00:00:39,920 --> 00:00:41,060 Open that up. 11 00:00:41,060 --> 00:00:44,000 Open the game manager if you don't have it open already. 12 00:00:44,340 --> 00:00:49,970 I'm going to go to the top of the game manager and I'm going to define two variables. 13 00:00:49,970 --> 00:00:58,340 Local DSS for data Store Service Game Get service data store service. 14 00:00:58,380 --> 00:01:01,490 I'm going to get another one called DS Data store. 15 00:01:01,520 --> 00:01:03,650 Got that from data store service. 16 00:01:04,730 --> 00:01:09,140 Get data store my data. 17 00:01:10,130 --> 00:01:10,400 Cool. 18 00:01:10,400 --> 00:01:11,870 So we got my data right here. 19 00:01:11,870 --> 00:01:14,150 That's going to be the name of the data store. 20 00:01:14,180 --> 00:01:16,940 This is going to be the variable to access the data store. 21 00:01:16,970 --> 00:01:23,270 Let's go down to above where we get our player added event and connect it to a function. 22 00:01:23,990 --> 00:01:26,860 Let's make another function local function. 23 00:01:26,870 --> 00:01:33,920 I'll call this initialize player data and we'll pass a player in. 24 00:01:34,760 --> 00:01:38,060 Then let's make a variable called data. 25 00:01:38,060 --> 00:01:39,440 We'll just initialize it to nil. 26 00:01:39,440 --> 00:01:43,310 For now I'm going to do something a little different. 27 00:01:43,610 --> 00:01:47,930 I do local, but I'm going to actually initialize two variables here. 28 00:01:47,930 --> 00:01:56,150 I'm going to have success and a message and we're going to initialize this by doing a protected call 29 00:01:56,420 --> 00:01:58,700 with an anonymous function. 30 00:01:58,700 --> 00:02:01,280 Nothing's going to get passed into that function. 31 00:02:01,280 --> 00:02:08,180 We're going to get our data back by doing a DS, get async. 32 00:02:08,180 --> 00:02:16,340 That's an asynchronous call to the data store and we'll use player user ID as the key. 33 00:02:16,340 --> 00:02:21,950 So we're going to go to our data store, we're going to get the data for the user, we're going to key 34 00:02:21,950 --> 00:02:24,980 off its ID and then it's going to come back. 35 00:02:24,980 --> 00:02:28,850 If we save data off, why put this in a P call? 36 00:02:28,850 --> 00:02:30,710 Well, this is an asynchronous call. 37 00:02:30,710 --> 00:02:32,410 It's an HTTP call. 38 00:02:32,420 --> 00:02:34,160 We might have some problems. 39 00:02:34,160 --> 00:02:38,150 If we do, we're going to get a failure and then we're going to get a reason for the failure. 40 00:02:38,150 --> 00:02:43,400 So it's a little bit of extra overhead, but you don't want to mess this up and then people have a whole 41 00:02:43,400 --> 00:02:47,840 bunch of data saved off and then they rage because there's a there's a mess up. 42 00:02:47,840 --> 00:02:54,680 So now what we need to do is check to see if the call was not successful. 43 00:02:54,680 --> 00:03:02,180 So I'll say, if not success, then player kick the player because it's better to kick him out and have 44 00:03:02,180 --> 00:03:08,420 him get back in than to write bad data to to his character. 45 00:03:08,420 --> 00:03:11,360 Because then if he leaves, we're going to save off bad data. 46 00:03:11,390 --> 00:03:13,160 All of his stuff is ruined. 47 00:03:13,160 --> 00:03:19,610 So kick them data store on reachable. 48 00:03:20,540 --> 00:03:23,870 Please try again. 49 00:03:24,050 --> 00:03:28,820 So give him give him some information and then put that message there. 50 00:03:30,080 --> 00:03:30,290 All right. 51 00:03:30,290 --> 00:03:32,590 That's going to give a little dialogue to pops up. 52 00:03:32,600 --> 00:03:35,540 He gets kicked or she gets kicked, either one. 53 00:03:35,540 --> 00:03:37,970 And then you look up and you're like, Oh, what's going on? 54 00:03:37,970 --> 00:03:39,740 Oh, a data store is unreachable. 55 00:03:39,740 --> 00:03:42,080 A good thing I didn't join the game and ruined my data. 56 00:03:42,080 --> 00:03:46,460 So now we get down here because we haven't been kicked. 57 00:03:46,880 --> 00:03:57,350 Say if data oh, we got data will say player leader stats, we got points and there's a value associated 58 00:03:57,350 --> 00:03:57,770 with that. 59 00:03:57,770 --> 00:04:02,060 So dot value that will be in the data points. 60 00:04:02,810 --> 00:04:03,950 Yeah, there we go. 61 00:04:05,270 --> 00:04:07,610 And we got kills and deaths. 62 00:04:07,610 --> 00:04:09,020 Let's save this off. 63 00:04:09,530 --> 00:04:10,010 That way. 64 00:04:10,010 --> 00:04:12,110 I'll speed things up a little bit. 65 00:04:13,190 --> 00:04:19,730 We're going to change this to kills and then data will have kills. 66 00:04:19,730 --> 00:04:20,660 It doesn't yet. 67 00:04:20,660 --> 00:04:22,520 So you'll be like, how does data have kills? 68 00:04:22,520 --> 00:04:25,310 Well, we don't know that yet, but I'm going to show you in a minute. 69 00:04:25,490 --> 00:04:30,050 And then deaths that I smell, deaths, right? 70 00:04:30,050 --> 00:04:31,040 Yes. 71 00:04:32,960 --> 00:04:37,450 What matters is if you spell it the same as up here, Right? 72 00:04:37,550 --> 00:04:39,320 Because that's your leaderboard data. 73 00:04:39,950 --> 00:04:40,310 All right. 74 00:04:40,310 --> 00:04:43,460 So that we had already saved data off. 75 00:04:43,460 --> 00:04:48,470 We successfully got the data back, but it might be your first time playing. 76 00:04:48,470 --> 00:04:49,840 There might not be data. 77 00:04:49,850 --> 00:05:00,170 So what we're going to do is we're going to say else data equals points equals zero, or make a little 78 00:05:00,170 --> 00:05:00,560 table. 79 00:05:00,560 --> 00:05:09,860 A key value pair table kills equals zero, and then deaths equals zero. 80 00:05:09,860 --> 00:05:14,060 So now table will have points, kills and deaths. 81 00:05:14,060 --> 00:05:19,430 They'll all equal zero, but it will have those those keys in there for our key value pair. 82 00:05:19,700 --> 00:05:26,630 All right, We get down to here, we get down to the bottom, we're going to say data store set async. 83 00:05:26,630 --> 00:05:28,340 So we'll do a save. 84 00:05:28,340 --> 00:05:35,150 When people enter the game, we're going to get the data and we'll just do a save here using the player's 85 00:05:35,150 --> 00:05:37,670 user ID and the data that we got. 86 00:05:37,670 --> 00:05:42,020 That's why it's really important or one of the reasons why it's really important to kick the player. 87 00:05:42,020 --> 00:05:48,020 If the player is bad, if the data is bad, because if the data comes in at zero or not, correct, 88 00:05:48,020 --> 00:05:50,900 we override his data, we save it off. 89 00:05:50,900 --> 00:05:53,720 Everything that would have been in here is gone. 90 00:05:53,720 --> 00:05:56,210 So that's why we use protected calls. 91 00:05:56,210 --> 00:06:01,790 I didn't use the protected call here because we're going to do periodic calls to the data store anyway. 92 00:06:01,790 --> 00:06:05,960 And if it doesn't save right away, we are going to be able to save it. 93 00:06:07,280 --> 00:06:11,730 This area in here is going to be called for first time players. 94 00:06:11,750 --> 00:06:18,020 So one other thing I'm going to do is put a note saying something like, maybe we could fire off a welcome 95 00:06:18,020 --> 00:06:18,770 screen, right? 96 00:06:18,770 --> 00:06:27,260 Maybe do a remote event, maybe do a remote event to a welcome screen. 97 00:06:30,380 --> 00:06:38,180 And we could print something to saying first time player, first time player, and we'll we'll remove 98 00:06:38,180 --> 00:06:41,970 that after we test this out and are sure that everything's going to work. 99 00:06:41,990 --> 00:06:44,030 Now, where does this get called? 100 00:06:44,210 --> 00:06:46,470 We have this initialized player data. 101 00:06:46,490 --> 00:06:47,840 Let's copy that. 102 00:06:48,620 --> 00:06:53,690 Let's take a look here right where the player is added. 103 00:06:53,900 --> 00:07:00,140 But before the character added and the character appearance loaded, we have to make sure there's a 104 00:07:00,140 --> 00:07:01,490 board on the player. 105 00:07:01,490 --> 00:07:06,950 So go below there because we're going to use that board to initialize the player data. 106 00:07:07,310 --> 00:07:12,170 I'm going to do a control V, I'm going to put initialized player data right here. 107 00:07:12,590 --> 00:07:14,250 All right, so let's try this out. 108 00:07:14,270 --> 00:07:18,180 Keep in mind, we can't really save anything off. 109 00:07:18,260 --> 00:07:24,830 We do an initial save here, but we can determine if it's the first time playing or not because there'll 110 00:07:24,830 --> 00:07:25,940 be no data store. 111 00:07:25,970 --> 00:07:30,620 Then I'm going to induce an error to see if our kick actually works. 112 00:07:30,650 --> 00:07:32,380 Let's hit our play button. 113 00:07:32,390 --> 00:07:32,660 Oh. 114 00:07:32,660 --> 00:07:37,400 Before we do that, let's go to view hit the output window so we could see any errors. 115 00:07:37,430 --> 00:07:41,660 Now I'm going to go hit my play button, this little tiny one right here. 116 00:07:42,730 --> 00:07:45,470 And we should see first time player message. 117 00:07:45,950 --> 00:07:46,850 There it is. 118 00:07:46,850 --> 00:07:48,450 First time player. 119 00:07:48,470 --> 00:07:50,780 Let's go ahead and turn the game off. 120 00:07:52,900 --> 00:07:58,120 And then we're going to play it again and we should not see first time player. 121 00:08:01,860 --> 00:08:03,390 And we don't. 122 00:08:03,510 --> 00:08:04,530 That's cool. 123 00:08:04,530 --> 00:08:06,230 We don't see the first time player. 124 00:08:06,240 --> 00:08:11,700 Let's induce an error to make sure our P call kicks us if we have a problem. 125 00:08:12,150 --> 00:08:18,330 Let's go to home Game settings Security. 126 00:08:18,330 --> 00:08:25,740 I'm going to turn off enable API access or enable studio access to API services. 127 00:08:25,740 --> 00:08:28,150 That way we're going to have a problem. 128 00:08:28,170 --> 00:08:29,820 Let's go ahead and play this. 129 00:08:32,710 --> 00:08:33,310 Boom. 130 00:08:33,310 --> 00:08:33,940 Look at that. 131 00:08:33,940 --> 00:08:35,100 This connected. 132 00:08:35,110 --> 00:08:39,130 You were kicked from this experience data store unreachable. 133 00:08:39,130 --> 00:08:40,660 Please try again. 134 00:08:40,660 --> 00:08:42,790 And then we got a little message down here. 135 00:08:42,820 --> 00:08:43,750 Perfect. 136 00:08:43,750 --> 00:08:44,530 That's what we want. 137 00:08:44,530 --> 00:08:49,480 We want to make sure that people can't get in and mess up their data if it's not right. 138 00:08:49,480 --> 00:08:58,540 Let's go back to our home game Settings Security Enable studio access to API services. 139 00:08:58,540 --> 00:08:59,870 And we're good. 140 00:08:59,890 --> 00:09:02,470 Now I want to show you one more thing. 141 00:09:02,470 --> 00:09:05,980 What happens if you save data off? 142 00:09:06,310 --> 00:09:11,410 But now the data is in the data store and you want to do first time entry stuff. 143 00:09:11,410 --> 00:09:13,780 How do you clear the data store? 144 00:09:13,780 --> 00:09:21,700 Well, you have to send a set, a sync for the user data, and I'm just doing this for each user. 145 00:09:23,070 --> 00:09:24,120 As false. 146 00:09:24,120 --> 00:09:26,340 This is going to clear my data. 147 00:09:26,640 --> 00:09:28,260 And this is the only place I'm saving it. 148 00:09:28,260 --> 00:09:29,340 So I'm going to enter the game. 149 00:09:29,340 --> 00:09:32,040 I'm going to have data, I'm going to leave the game. 150 00:09:32,040 --> 00:09:34,920 This is going to be the last time we talked to the data store. 151 00:09:34,920 --> 00:09:37,670 We're setting our user ID data to false. 152 00:09:37,680 --> 00:09:39,750 That's essentially clearing it out. 153 00:09:40,050 --> 00:09:41,730 Let's take a look at our view tab, though. 154 00:09:43,730 --> 00:09:46,580 We're not going to see anything because it wasn't the first time player. 155 00:09:47,030 --> 00:09:49,070 Now let's exit. 156 00:09:51,400 --> 00:09:54,600 Let's go ahead and change this back because we cleared the data store. 157 00:09:54,610 --> 00:09:56,650 We want it to work regular now. 158 00:09:57,010 --> 00:10:02,260 But now it should be the first time playing because for this player, because we cleared his data. 159 00:10:06,780 --> 00:10:08,330 First time player. 160 00:10:08,340 --> 00:10:09,390 There we go. 161 00:10:09,390 --> 00:10:13,590 So that's how you clear the data for the data store for individual players. 162 00:10:13,590 --> 00:10:19,050 Anyway, if you want to scrap the entire data store, you may want to just go ahead and make another 163 00:10:19,050 --> 00:10:22,350 data store right here, maybe version two or something.